package com.judeDot;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.hc.bean.Dot;
import com.hc.util.CommonData;

public class MapConfigUtil {

	
	public static int[][] dotArray ;
	public static Image mapImage;
	
	
public static void readConfigFile(String filePath,int hight,int width){
	

	dotArray = new int[hight][width];

	File file = new File(filePath);
	FileReader fileReader =null;
	BufferedReader reader  = null;
	try {
		fileReader= new FileReader(file);
		
		reader= new BufferedReader(fileReader);
		String lin = "";
		
		String xlin = "";
		String ylin="";
		int i =0;
		try {
			while((lin = reader.readLine())!=null){
				if(i<hight){
					byte[] bs = lin.getBytes();
					for(int j =0;j<bs.length;j++){
						dotArray[i][j] =  bs[j];
					}
				}
				if(i==(hight)){
					xlin = lin;
				}else if(i==(hight+1)){
					ylin = lin;
				}
				i++;
			}
			
			String[] xArray = xlin.split("_");
			String[] yArray = ylin.split("_");
			
			CommonData.apexDots.clear();
			
			for(int j =1 ; j <xArray.length;j++){
				CommonData.apexDots.add(new Dot(Integer.parseInt(xArray[j]), Integer.parseInt(yArray[j])));
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}finally{
		try {
			if(reader!=null){
				reader.close();
			}
			if(fileReader!=null){
				fileReader.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	
	
}	
//public static void main(String[] args) {
//	MapConfigUtil.readConfigFile("D:\\test.text");
//	
//	System.out.println(dotArray[1][44]);
//	
//}

public static boolean writeConfigFile(int map_hight,int map_width,String filePath){
	
	if(dotArray==null){
	dotArray = new int[map_hight][map_width];
	}
	for(int map_i=0;map_i<map_hight;map_i++){
		for(int map_j=0;map_j<map_width;map_j++){
			dotArray[map_i][map_j] = 0;
		}
	}
	int size = PaintMapPanel.paintInfo.size();
	
	String x_str="";
	String y_str="";
	MyPoint p1,p2;
		for (int i = 0; i < size; i++) {
			if (i + 1 < size) {
				p1 = (MyPoint) PaintMapPanel.paintInfo.elementAt(i);
				p2 = (MyPoint) PaintMapPanel.paintInfo.elementAt(i + 1);
				for (int i_y = p1.y; i_y <= p2.y; i_y++) {
					for (int i_x = p1.x; i_x <= p2.x; i_x++) {
						dotArray[i_y][i_x] = 1;
					}
				}
				i = i + 1;
				
				x_str+="_"+p1.x+"_"+p2.x+"_"+p1.x+"_"+p2.x;
				y_str+="_"+p1.y+"_"+p1.y+"_"+p2.y+"_"+p2.y;
				
			}
		}
	
	StringBuilder sb = new StringBuilder();
	for(int map_i=0;map_i<map_hight;map_i++){
		for(int map_j=0;map_j<map_width;map_j++){
			sb.append("").append(dotArray[map_i][map_j]);
	
		}
		sb.append("\r\n");
	}
	
	sb.append(x_str).append("\r\n").append(y_str).append("\r\n");
	
	
	FileWriter w =null;
	BufferedWriter bf = null;
	try {
		File file = new File(filePath);
		w = new FileWriter(file);
		bf = new BufferedWriter(w);
		bf.write(sb.toString());
		

		
	} catch (IOException e) {
		e.printStackTrace();
		return false;
	}finally{
		
		try {
			
			if(bf!=null){
				bf.flush();
				bf.close();
			}
			if(w!=null){
				w.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	return true;
}	
	
}
